package com.jworkbench.jflight.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import com.jworkbench.jflight.domain.Account;
import com.jworkbench.jflight.exception.AccountNotFoundException;
import com.jworkbench.jflight.service.AccountService;

public class ViewAccountController extends MultiActionController {

	private AccountService accountService;

	public ViewAccountController() throws ApplicationContextException {
		setSupportedMethods(new String[] { METHOD_GET });
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public ModelAndView accountNotFound(HttpServletRequest request,
			HttpServletResponse response, AccountNotFoundException e) {
		List<String> errorMessages = new ArrayList<String>();
		errorMessages.add("No account found for "
				+ request.getParameter("searchBy"));
		return new ModelAndView("accountFindError", "errorMessages",
				errorMessages);
	}

	public ModelAndView findByUsername(HttpServletRequest request,
			HttpServletResponse response, SearchCriteria criteria)
			throws NoSuchRequestHandlingMethodException {
		ensureMethod(request, METHOD_GET);
		Account account = accountService.findAccountByUsername(criteria
				.getSearchBy());
		return new ModelAndView("viewAccount", "account", account);
	}

	public ModelAndView findByFirstName(HttpServletRequest request,
			HttpServletResponse response, SearchCriteria criteria) {
		List<Account> accounts = accountService
				.findAccountsByFirstName(criteria.getSearchBy());
		return new ModelAndView("viewAccounts", "accounts", accounts);
	}

	public ModelAndView findByLastName(HttpServletRequest request,
			HttpServletResponse response, SearchCriteria criteria) {
		List<Account> accounts = accountService.findAccountsByLastName(criteria
				.getSearchBy());
		return new ModelAndView("viewAccounts", "accounts", accounts);
	}

	public static class SearchCriteria {
		private String searchBy;

		public String getSearchBy() {
			return searchBy;
		}

		public void setSearchBy(String searchBy) {
			this.searchBy = searchBy;
		}
	}

	private void ensureMethod(HttpServletRequest request, String... methods)
			throws NoSuchRequestHandlingMethodException {
		for (String method : methods) {
			if (request.getMethod().equals(method)) {
				return;
			}
		}
		throw new NoSuchRequestHandlingMethodException(request);
	}
}
